﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using ENail.Domain.Core;
using ENail.Domain.Core.Contract.Services;
using Equation.Enterprise.Framework.Domain.Data;
using Equation.Enterprise.Utilities.DesignByContract;


namespace ENail.ApplicationServices
{
    public class TaskService : ITaskService
    {
        private IRepository<Employee> _employeeRepository;
        private IRepository<Assignment> _assignmentRepository; 
        private IRepository<Registration> _registrationRepository;
        private IRepository<NailService> _nailServiceRepository;


        public TaskService(IRepository<Employee> employeeRepository, IRepository<Assignment> assignmentRepository, IRepository<Registration> registrationRepository, IRepository<NailService> nailServiceRepository)
        {
            Check.Require(employeeRepository!=null);
            Check.Require(assignmentRepository!=null);
            this._employeeRepository = employeeRepository;
            this._assignmentRepository = assignmentRepository;
            _registrationRepository = registrationRepository;
            _nailServiceRepository = nailServiceRepository;
        }
        public IList<Task> GetTask(DateTime startTime, DateTime endTime)
        {
            var list = (from e in _employeeRepository.List()
                        join s in _nailServiceRepository.List() on -1 equals -1
                        //on e.Id equals asm.Employee.Id
                        //where asm.IsActive
                        select new Task
                                   {
                                       FirstName = e.FirstName,
                                       LastName = e.LastName,
                                       Service=s.ServiceName,
                                       Made = (from a in _assignmentRepository.List()
                                               where a.Registration.IsActive &&
                                                     a.Registration.Status == Status.Closed &&
                                                     a.Registration.StartTime >= startTime && a.Registration.EndTime <= endTime &&
                                                     a.Employee.Id == e.Id && a.NailService.Id==s.Id
                                                     //a.Registration.Id == asm.Registration.Id
                                               select a).Count(),
                                       Waiting = (from a in _assignmentRepository.List()
                                               where a.Registration.IsActive &&
                                                     a.Registration.Status == Status.Opened &&
                                                     a.Registration.StartTime >= startTime && a.Registration.EndTime <= endTime &&
                                                      a.Employee.Id == e.Id && a.NailService.Id == s.Id
                                                     //a.Employee.Id == e.Id //&&
                                                     //a.Registration.Id == asm.Registration.Id
                                               select a).Count()

                                   }).OrderBy(x=>x.FirstName+x.LastName).ToList();

            return list;
        }
        public IList<Task> GetTask(string date)
        {
            DateTime fromdDate  = System.DateTime.Parse(date);
            DateTime toDate = fromdDate.AddDays(1);
            return GetTask(fromdDate, toDate);
        }
        public IList<Task> GetTask(DateTime dateTime)
        {
            return GetTask(dateTime.ToShortDateString());
        }
    }
}
